//
//  main.cpp
//  SocketNIO
//
//  Created by dlleng on 2023/7/23.
//

#include <iostream>
#include <thread>
#include <future>

#include "ClientChannel.h"
#include "ServerChannel.h"

using namespace nio;

class Test: public ChannelDelegate{
private:
    ClientChannel* client = nullptr;
    ServerChannel* server = nullptr;
public:
    Test() {
        client = new ClientChannel(this);
        //client->connect("127.0.0.1", 8888);
        client->connect("www.apple.com", 80);
        //server = new ServerChannel(this);
        //server->startServer("127.0.0.1", 8887);
        client->eventLoop->execute(1000, false, []() {
            std::cout << "1秒任务" << std::endl;
            });
//        client->eventLoop->execute(3000, false, []() {
//            std::cout << "3秒任务" << std::endl;
//            });
//        client->eventLoop->execute(5000, false, []() {
//            std::cout << "5秒任务" << std::endl;
//            });
    }

    void send(char* buf, int size) {
        if (client == nullptr)return;
        std::string data(buf, size);
        client->write(data);
    }

    virtual void onConnect(ClientChannel* channel, std::string host, int port) {
        std::cout << "连接成功:" << host << " port:" << port << std::endl;
        std::cout << "本地地址:" << channel->getLocalAddress().toString() << std::endl;
        std::cout << "远端地址:" << channel->getRemoteAddress().toString() << std::endl;
    }
    virtual void onDisconnect(ClientChannel* channel, SocketError* error) {
        std::cout << "断开连接";
        if (error == nullptr) std::cout << std::endl;
        else std::cout << error->toString() << std::endl;

        std::cout << "本地地址:" << channel->getLocalAddress().toString() << std::endl;
        std::cout << "远端地址:" << channel->getRemoteAddress().toString() << std::endl;
    }
    virtual void onWrite(ClientChannel* channel, std::string& data) {

    }
    virtual void onRead(ClientChannel* channel, std::string& data) {
        std::cout << "收到：" << data << std::endl;
    }

    virtual void onAccept(ClientChannel* channel) {
        std::cout << "客户端连接成功" << std::endl;
        this->client = channel;
    }
};

void test1();
//GET / HTTP/1.2\r\nHost: www.apple.com\r\n\r\n
int main(int argc, const char * argv[])
{
    Test t;

    while (true) {
        char buf[64];
        memset(buf, 0, 64);
        fgets(buf, 64, stdin);
        strcpy(buf, "GET / HTTP/1.2\r\nHost: www.apple.com\r\n\r\n");
        int len = strlen(buf);
        t.send(buf, len);
    }
    
    return 0;
}

void func1() {
    auto sock = Socket(SocketFamily::inet, SocketType::dgram);
    sock.Bind("127.0.0.1", 9999);

    auto t = std::thread([&sock]() {
        while (1) {
            char recvbuf[64];
            memset(recvbuf, 0, 64);
            sockaddr_storage clientAddr;
            int addrLen = sizeof(clientAddr);

            int ret = sock.Recvfrom(recvbuf, 64, (sockaddr*)&clientAddr, &addrLen);
            if (ret < 0) {
                std::cout << "接收失败：" << ret << " errno:" << Errno << std::endl;
                //return -1;
            }
            else {
                std::cout << "接收成功：(" << ret << ")" << recvbuf << std::endl;
            }
        }
        });

    sock.Connect("127.0.0.1", 9999);

    char buf[64];
    while (true) {
        memset(buf, 0, 64);
        fgets(buf, 64, stdin);
        int len = strlen(buf);
        int num = sock.Send(buf, len, 0);
        std::cout << "发送：" << num << std::endl;
    }
}
